function WebAppController(){
	
	this.view = {
		use: "base.html", 
		put: {
			header: "sections/header.html",
			footer: "sections/footer.html"
		}
		
	}
	
	this.handler = function(){
		var userObj = db.users.getSingle({cookie: request.cookies.user}),
			htmlForm, loginForm = {}, registerForm = {};
			
		if(!userObj){
			htmlForm = new HTMLForm.generator(request);
			loginForm = htmlForm.create({
				id: "loginForm",
				method: "post",
				inputs: [
					{"username": {label: "username:", validate: {required: true}}},
					{"password": {label: "password:", type: "password",  validate: {required: true}}},
					{"submit_": {value: "login", type: "submit"}}
				]
			});
			
			registerForm = htmlForm.create({
			id: "registerForm",
			method: "post",
			inputs: [
				{"username": {label: "username:", validate: {required: true, minlength: 3, maxlength: 15}}},
				{"password": {label: "password:", type: "password",  validate: {required: true, minlength: 5}}},
				{"confirm-password_": {label: "verify password:", type: "password", value: "", validate: {test: function(val, values){return (val == values["password"])}}}},
				{"submit_": {value: "register", type: "submit"}}
			]
		});
		} else {
			request.setAttr("user", userObj.value);
		}
		this.get = function(showErrors, errorMsg){
			
			if(userObj){
				$("#header").append("<ul><li><a href='account'>account</a></li>" +
					"<li><a href='/ashafa/logout'>logout</a></li></ul>");
				
			} else {
				errorMsg = (errorMsg) ? "<p class='error'>" + errorMsg + "</p>" : "";
				
				$("#sidenav").append(errorMsg + "<h3>Login</h3>" + 
					loginForm.html(showErrors) + 
					"<h3 class='divided'>Register</h3>" +
					registerForm.html(showErrors));
			}
		}
		
		this.post = function(){
			var errorMsg;
			if(loginForm.pass){
				var userObj = db.users.getSingle({username: loginForm.values["username"].toLowerCase()});
				if(userObj && Crypt.check(loginForm.values["password"], userObj.value.password)){
					var rs, user = userObj.value;
					if(user.cookie == "x"){
						while(rs == null || db.users.getSingle({cookie: rs})){
							rs = (new RandomString.generator()).generate(8);
						}
						user.cookie = rs;
					}
					response.setCookie({
						name: "user",
						value: rs || user.cookie,
						path: "/",
						expires: (new Date()).addYears(3)
					});
					db.users.update(user, {username: user.username});
					response.redirect("");
				} else {
					errorMsg = "Bad login."
				}
			}
			
			if(registerForm.pass){
				if(!db.users.getSingle({username: registerForm.values["username"]})){
					var rs;
					while(rs == null || db.users.getSingle({cookie: rs})){
						rs = (new RandomString.generator()).generate(8);
						alert(rs);
					}
					db.users.insert({
						username: registerForm.values["username"].toLowerCase(),
						displayName: registerForm.values["username"],
						password: Crypt.hash(registerForm.values["password"]),
						cookie: rs,
						services: [],
						owns :[],
						registerDate: new Date(),
						lastLoginDate: new Date()
					});
					response.setCookie({
						name: "user",
						value: rs,
						path: "/",
						expires: (new Date()).addYears(3)
					});
					response.redirect("");
				} else {
					registerForm.addError("username", "username already exists.")
				}
			}
			this.get(!loginForm.pass || !registerForm.pass, errorMsg);
		}
	}

}